import { _decorator, Component, instantiate, Label, Node, Prefab } from 'cc';
import { Rank, RankUser } from '../entity/Rank';
import { PrefabEnum } from '../common/PrefabEnum';
import { UIManager } from '../common/UIManager';
import UserApi from '../api/UserApi';
import { Village } from '../entity/Village';
import { RankDetailVillage } from './RankDetailVillage';
import AllyApi from '../api/AllyApi';
import { AllyOperateType } from '../entity/Ally';
import { EventManager } from '../event/EventManager';
import { AllyUser } from '../entity/AllyUser';
const { ccclass, property } = _decorator;

@ccclass('RankDetail')
export class RankDetail extends Component {
    @property(Node)
    close:Node
    @property(Node)
    content:Node
    @property(Label)
    username:Label
    @property(Label)
    race:Label
    @property(Label)
    villageNum:Label
    @property(Label)
    people:Label
    @property(Label)
    rank:Label
    @property(Label)
    union:Label
    @property(Label)
    job:Label
    @property(Node)
    kickout:Node
    @property(Prefab)
    rankDetailVillage:Prefab

    private userId:number
    private userName:string

    start() {
        this.close.on(Node.EventType.TOUCH_END, this.onClose, this)
        this.kickout.on(Node.EventType.TOUCH_END, this.onKickout, this)
    }

    async setData(uid:number) {
        this.userId = uid
       
        await UIManager.instance.showLoading()
        let res = await UserApi.Execute(uid)
        UIManager.instance.closeLoading()
        console.log(res)
        let data:RankUser = res.data
        this.username.string = data.UserName
        this.race.string = data.RaceName
        this.villageNum.string = data.VillageNum + ''
        this.people.string = data.People + ''
        this.rank.string = data.Rank + ''
        this.union.string = data.Ally.allyCutName

        for(let item of data.VillageList){
            let v = new Village()
            v.people = item.People
            v.name = item.VillageName
            v.x = item.X
            v.y = item.Y
            v.isMainVillage = item.isMainVillage == 1

            let node = instantiate(this.rankDetailVillage)
            this.content.addChild(node)
            node.getComponent(RankDetailVillage).setData(v)
        }

        this.userId = uid
        this.userName = data.UserName
    }

    onClose(){
        UIManager.instance.closeUI(PrefabEnum.RankDetail)
    }

    async onKickout(){
        try {
            await UIManager.instance.modal('提示', `您确定将${this.userName}踢出联盟吗？`,)
            await UIManager.instance.showLoading()
            let res = await AllyApi.Execute({
                op: AllyOperateType.Kickout,
                uid: this.userId
            })
            UIManager.instance.closeLoading()
            console.log(res)
            let u = new AllyUser()
            u.UserId = this.userId
            EventManager.instance.emit(EventManager.EventType.KickoutAlly, u)

            this.onClose()
        } catch (error) {

        }
    }
}


